import re
from src.core import *
from typing import List
from src.db import dbm, dbs
from typing import Annotated
from pydantic import BaseModel
from src.core.variables.constant import *
from pydantic import Field, AfterValidator


class _Validate:
    @staticmethod
    def on_empty(v: str) -> str:
        if not v.strip():
            if v not in [""]:
                log.ERROR(f"字段值不能为空: {v}")
                raise ValueError(f"字段值不能为空")
        return v

    @staticmethod
    def name(v: str) -> str:
        with dbm.get("sys_default") as db_session:
            from sqlalchemy.orm import scoped_session
            db_session: scoped_session
            if db_session.query(dbs.sys_default.Role).filter(
                dbs.sys_default.Role.name == v
            ):
                raise ValueError("角色名重复！")
            return v

    @staticmethod
    def description(v: str) -> str:
        ...
        return v

    @staticmethod
    def status(v: int) -> str:
        if v not in [0, 1]:
            raise ValueError("状态错误！")
        return v

    @staticmethod
    def uid(v: str) -> str:
        with dbm.get("sys_default") as db_session:
            from sqlalchemy.orm import scoped_session
            db_session: scoped_session
            db_result = db_session.query(dbs.sys_default.Role).filter(
                dbs.sys_default.Role.uid == v
            )
            if len(db_result) > 1:
                log.ERROR(f"角色ID重复，请检查数据库！")
                raise ValueError("ID重复！请联系管理员")
            elif len(db_result) == 0:
                raise ValueError("ID不存在！")
            return v


class add_role_schema(BaseModel):
    """添加角色"""

    name: Annotated[
        str, AfterValidator(_Validate.on_empty), AfterValidator(_Validate.name)
    ] = Field(..., description="角色名")
    description: Annotated[
        str, AfterValidator(_Validate.on_empty), AfterValidator(_Validate.description)
    ] = Field(..., description="角色描述")
    status: Annotated[
        str, AfterValidator(_Validate.on_empty), AfterValidator(_Validate.status)
    ] = Field(..., description="角色状态")


class roles_list_schema(BaseModel):
    """角色列表"""

    ...


class remove_role_schema(BaseModel):
    """删除角色"""

    uid: Annotated[
        str, AfterValidator(_Validate.on_empty), AfterValidator(_Validate.uid)
    ] = Field(..., description="角色ID")


class role_update_schema(BaseModel):
    """更新角色"""

    uid: Annotated[
        str, AfterValidator(_Validate.on_empty), AfterValidator(_Validate.uid)
    ] = Field(..., description="角色ID")
    name: Annotated[
        str, AfterValidator(_Validate.on_empty), AfterValidator(_Validate.name)
    ] = Field(..., description="角色名")
    description: Annotated[
        str, AfterValidator(_Validate.on_empty), AfterValidator(_Validate.description)
    ] = Field(..., description="角色描述")
    status: Annotated[
        str, AfterValidator(_Validate.on_empty), AfterValidator(_Validate.status)
    ] = Field(..., description="角色状态")


class role_info_schema(BaseModel):
    """角色信息"""

    uid: Annotated[
        str, AfterValidator(_Validate.on_empty), AfterValidator(_Validate.uid)
    ] = Field(..., description="角色ID")


# class update_role_schema(BaseModel):
#     """更新角色权限"""
